<?php

namespace App\Http\Controllers;

use App\Models\Comment;
use App\Models\Country;
use App\Models\Post;
use App\Models\Role;
use App\Models\Tag;
use App\Models\UserAccount;
use App\Models\Video;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrmController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        /*$account = User::find(1)->account;
        dd($account);*/
        /*$user = UserAccount::find(1)->user;
        dd($user);*/
//        $posts = User::find('1')->posts;
        /*$posts = User::find(1)->posts()->where('views','>',100)->get();
        dd($posts);*/
        /*$author = Post::find(2)->user;
        dd($author);*/
        /*$user = User::find(1);
        $roles = $user->roles;
        echo 'User#'.$user->name.'所拥有的角色：<br>';
        foreach ($roles as $role)
        {
            echo $role->name.'<br>';
        }*/
        /*$role = Role::find(4);
        $users = $role->users;
        echo 'Role#'.$role->name.'下面的用户：<br>';
        foreach ($users as $user) {
            echo $user->name.'<br>';
        }*/
        /*$roles = User::find(1)->roles;
        foreach ($roles as $role) {
            echo $role->pivot->role_id.'<br>';
        }*/

        //远程一对多
       /* $country = Country::find(1);
        $posts = $country->posts;

        echo 'Country#'.$country->name.'下的文章：<br>';
        foreach($posts as $post){
            echo '&lt;&lt;'.$post->title.'&gt;&gt;<br>';
        }*/

        //多态关联
        /*$video = Video::find(1);
        $videoComments = $video->comments;
        dd($videoComments);*/
        /*$comment = Comment::find(2);
        $item = $comment->item;
        dd($item);*/

        //多对多多态关联
        /*$post = Post::find(5);
        $tags = $post->tags;
        dd($tags);*/
//        $tag = Tag::find(1);
//        $posts = $tag->posts;
//        dd($posts);
       /* 定义修改器 $user = User::find(1);
        $user->name = 'Sally';
       dd($user);*/

        //属性修改器
        /*$user = User::find(2);
        dd($user->created_at->getTimestamp());
        $options = $user->options;
        $options['key2'] = 'value2';
        $user->options = $options;
        $user->save();*/
       // $user = User::first();
        $user  = User::with('posts')->find(1);
        dump($user->toArray() );
        return view('welcome');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
